using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System;

[Serializable]
public class GT_AddNode : GT_NodeBase
{
    #region Variables
    public float nodeSum;
    public GT_NodeOutput output;
    public GT_NodeInput inputA;
    public GT_NodeInput inputB;
    public float returnValue = 0.0f;
    #endregion

    #region Constrcutor
    public GT_AddNode()
    {
        output = new GT_NodeOutput();
        inputA = new GT_NodeInput();
        inputB = new GT_NodeInput();
    }
    #endregion

    #region Main Methods
    public override void InitNode()
    {
        base.InitNode();
        nodeType = NodeType.Add;
        nodeRect = new Rect(10f, 10f, 150f, 65f);
    }

    public override void UpdateNodes(Event e, Rect viewRect)
    {
        base.UpdateNodes(e, viewRect);
    }

#if UNITY_EDITOR
    public override void UpdateNodeGUI(Event e, Rect viewRect, GUISkin viewSkin)
    {
        base.UpdateNodeGUI(e, viewRect, viewSkin);

        //Output
        if (GUI.Button(new Rect(nodeRect.x + nodeRect.width, nodeRect.y + (nodeRect.height * 0.5F) - 12F, 24F, 24F), "", viewSkin.GetStyle("NodeOutput")))
        {
            if (parentGraph != null)
            {
                parentGraph.wantsConnection = true;
                parentGraph.connectionNode = this;
            }
        }

        //Input A
        if (GUI.Button(new Rect(nodeRect.x - 24F, (nodeRect.y + (nodeRect.height * 0.33F) * 2F) - 14F, 24F, 24F), "", viewSkin.GetStyle("NodeInput")))
        {
            if (parentGraph != null)
            {
                inputA.inputNode = parentGraph.connectionNode;
                inputA.isOccupied = inputA.inputNode != null;

                parentGraph.wantsConnection = false;
                parentGraph.connectionNode = null;
            }
        }

        //Input B
        if (GUI.Button(new Rect(nodeRect.x - 24F, (nodeRect.y + (nodeRect.height * 0.33F) * 2F) - 8F, 24F, 24F), "", viewSkin.GetStyle("NodeInput")))
        {
            if (parentGraph != null)
            {
                inputB.inputNode = parentGraph.connectionNode;
                inputB.isOccupied = inputB.inputNode != null;

                parentGraph.wantsConnection = false;
                parentGraph.connectionNode = null;
            }
        }

        if (inputA.isOccupied && inputB.isOccupied)
        {
            GT_FloatNode nodeA = (GT_FloatNode)inputA.inputNode;
            GT_FloatNode nodeB = (GT_FloatNode)inputB.inputNode;

            if (nodeA != null && nodeB != null)
            {
                nodeSum = nodeA.nodeValue + nodeB.nodeValue;
            }
        }
        else
        {
            nodeSum = 0.0f;
        }

        //Draw lines
        DrawInputLines();
    }

    public override void DrawNodeProperties()
    {
        base.DrawNodeProperties();

        EditorGUILayout.FloatField("Sum:", nodeSum);
    }
#endif
    #endregion

    #region Utility Methods

    private void DrawInputLines()
    {
        if (inputA.isOccupied && inputA.inputNode != null)
        {
            DrawLines(inputA, 1F);
        }
        else
        {
            inputA.isOccupied = false;
        }
        if (inputB.isOccupied && inputB.inputNode != null)
        {
            DrawLines(inputB, 2F);
        }
        else
        {
            inputB.isOccupied = false;
        }

    }

    private void DrawLines(GT_NodeInput curInput, float inputID)
    {
        Handles.BeginGUI();
        Handles.color = Color.white;
        Handles.DrawLine(
            new Vector3(
                curInput.inputNode.nodeRect.x + curInput.inputNode.nodeRect.width + 24f,
                curInput.inputNode.nodeRect.y + (curInput.inputNode.nodeRect.height * 0.5f),
            0f),
            new Vector3(nodeRect.x - 24f, (nodeRect.y + (nodeRect.height * 0.33f)*inputID), 0f));
        Handles.EndGUI();
    }

    #endregion
}
